package org.budo.dubbo.http.client.java;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;

import org.budo.dubbo.http.client.java.callback.ApiCallback;
import org.budo.dubbo.http.client.java.exception.ApiErrorException;
import org.budo.dubbo.http.client.java.impl.DefaultResponseHandler;
import org.budo.dubbo.http.client.java.impl.FastJsonSerialization;

import lombok.Setter;
import lombok.ToString;
import lombok.Getter;

/**
 * @author lmw-2
 */
@Getter
@Setter
@ToString
public class RequestContext {
    private static final DefaultResponseHandler RESPONSE_HANDLER_DEFAULT = new DefaultResponseHandler();

    private static final FastJsonSerialization SERIALIZATION_DEFAULT = new FastJsonSerialization();

    private static final ApiCallback API_CALLBACK_DEFAULT = new ApiCallback() {
        public void success(Object result, RequestContext request) {
            System.err.println("default success, result=" + result + ", request=" + request);
        }

        public void fail(ApiErrorException error, RequestContext request) {
            System.err.println("default fail, error=" + error + ", request=" + request);
        }
    };

    private Serialization serialization = SERIALIZATION_DEFAULT;

    private ApiCallback apiCallback = API_CALLBACK_DEFAULT;

    private ResponseHandler responseHandler = RESPONSE_HANDLER_DEFAULT;

    private Map<String, Object> headers = new HashMap<String, Object>();

    private Map<String, Object> parameters = new HashMap<String, Object>();

    /**
     * 是否异步
     */
    private Boolean async;

    private RequestFilter requestFilter;

    private String url;

    private ExecutorService executorService;

    private Transporter transporter;

    private String interfaceType;

    private String methodName;

    private Class<?> returnType;

    private Object[] arguments;

    private String requestBody;

    private String responseBody;

    private Throwable exception;

    public RequestContext withRequestContext(RequestContext request) {
        this.setSerialization(request.getSerialization());
        this.setApiCallback(request.getApiCallback());
        this.setResponseHandler(request.getResponseHandler());
        this.setHeaders(request.getHeaders());
        this.setParameters(request.getParameters());
        this.setUrl(request.getUrl());
        this.setTransporter(request.getTransporter());
        this.setExecutorService(request.getExecutorService());
        this.setInterfaceType(request.getInterfaceType());
        this.setMethodName(request.getMethodName());
        this.setReturnType(request.getReturnType());
        this.setArguments(request.getArguments());
        this.setRequestBody(request.getRequestBody());
        this.setRequestFilter(request.getRequestFilter());
        this.setResponseBody(request.getResponseBody());
        this.setAsync(request.getAsync());
        return this;
    }
}